package com.ogier.app.ihm;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import java.util.UUID;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import com.ogier.app.jeu.pieces.Piece;

public class PieceIHM extends JLabel {

	static final String			IMAGE_ONBOARD			= "_onboard";
	static final String			IMAGE_OUTOFBOARD		= "_outofboard";

	static final String			LABEL_BLANC				= "blanc";
	static final String			LABEL_NOIR				= "noir";

	static final String			EXTENSION_IMAGE_FILE	= ".png";

	private final Piece			laPiece;
	private URL					imgURLOnBoard;
	private URL					imgURLOutOfBoard;
	private CaseIHM				laCase;
	private final UUID			id;

	static final ClassLoader	loader					= PieceIHM.class.getClassLoader();

	public PieceIHM(Piece piece, CaseIHM laCase) {
		laPiece = piece;
		this.laCase = laCase;

		id = laPiece.getId();
		// System.out.println(loader.getResource("."));

		try {

			imgURLOnBoard = loader.getResource("./" + laPiece.getClass().getSimpleName() + "_" + getStringCouleur()
					+ IMAGE_ONBOARD + EXTENSION_IMAGE_FILE);
			imgURLOutOfBoard = loader.getResource("./" + laPiece.getClass().getSimpleName() + "_" + getStringCouleur()
					+ IMAGE_OUTOFBOARD + EXTENSION_IMAGE_FILE);

			setOnBoard();

			this.addMouseListener(new MouseListener() {

				public void mouseClicked(MouseEvent arg0) {

				}

				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub

				}

				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub

				}

				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub

				}

				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					onClick();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onClick() {

		laCase.getLePlateau().selectionnerPiece(laPiece, laCase);

	}

	public Piece getPiece() {
		return laPiece;
	}

	public void setPosition(CaseIHM laCase) {
		this.laCase = laCase;
	}

	public UUID getId() {
		return id;
	}

	public void setOnBoard() {
		this.setIcon(new ImageIcon(imgURLOnBoard));
	}

	public void setOutOfBoard() {
		this.setIcon(new ImageIcon(imgURLOutOfBoard));
	}

	public String getStringCouleur() {
		return (laPiece.getCouleur() == Piece.BLANC) ? LABEL_BLANC : LABEL_NOIR;
	}
}
